package npr;


import processing.core.*;

import javax.media.opengl.*;
import com.sun.opengl.util.*;


/**
 * Detects contours by rendering the backfacing polygons again in wireframe mode 
 * with given line width after the main rendering pass.
 * Unlike image space algorithms, this results in a guaranteed equal line width. 
 * Note that this detects contours alone, that is, edges connecting a front-facing and a back-facing polygon.
 */
public class WireframeContours extends HybridLines{

    /**
     * Shader for line colour.
     */
    protected GLSL color;

    /**
     * Creates a WireframeContours object and initializes it.
     */
    public WireframeContours(){

	color=new GLSL();
        color.loadVertexShader("color.vert");
        color.loadFragmentShader("color.frag");
        color.useShaders();
    }

    /**
     * Renders the backfacing polygons in wireframe mode with given line width.
     * You don't have to call this medhod. It is called by the renderer.
     */
    public void postProcess(){
	
	renderer= (NPR)parent.g;
	gl= renderer.gl;
	
	gl.glActiveTexture(GL.GL_TEXTURE0);
	
	gl.glLineWidth(width);
	gl.glPolygonMode(GL.GL_BACK, GL.GL_LINE);
	gl.glShadeModel(GL.GL_FLAT);
	gl.glDisable(GL.GL_NORMALIZE);
	//lighting is disabled by default
	//gl.glDisable(GL.GL_LIGHTING);
	gl.glEnable(GL.GL_CULL_FACE);
	gl.glCullFace(GL.GL_FRONT);

	color.startShader();
	int location;
	location= color.getUniformLocation("color");
	color.setFloatVec4(location, red, green, blue, alpha);
	render();
	color.endShader();

	gl.glLineWidth(1);
	gl.glPolygonMode(GL.GL_BACK, GL.GL_FILL);        
	gl.glShadeModel(GL.GL_SMOOTH);
	gl.glEnable(GL.GL_NORMALIZE);
	//lighting is disabled by default
	//gl.glEnable(GL.GL_LIGHTING);
	gl.glCullFace(GL.GL_BACK);	
	gl.glDisable(GL.GL_CULL_FACE);
    }
}